//
//  FLLSceneInfoViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/12.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLSceneInfoViewController.h"
#import "FLLInfoTableViewCell.h"
#import "FLLMacro.h"
#import "FLLInfoModel.h"
#import "InfoTags_Info.h"
#import "InfoData.h"
#import "InfoDishes_List.h"
#import "InfoRelates.h"
#import "DishSecData.h"
#import "FLLAllDishModel.h"
#import "DishData.h"
#import "DishTags_Info.h"
#import "FLLMakeFoodViewController.h"
#import "FLLSearchViewController.h"


@interface FLLSceneInfoViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *infoTableView;
@property (nonatomic, strong) FLLInfoModel *fllInfoModel;
@property (nonatomic, strong) NSMutableArray<InfoDishes_List *> *tableArray;
@property (nonatomic, strong) NSMutableArray<InfoTags_Info *> *collectionArray;
@property (nonatomic, strong) FLLAllDishModel *fllAllDishModel;
@property (nonatomic, strong) NSMutableArray<DishSecData *> *loadArray;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIImageView *whiteView;
@property (nonatomic, assign) BOOL isLoading;
@property (nonatomic, assign) NSInteger pageCount;
@property (nonatomic, copy) NSString *bodyString;
/* 自定义导航栏 */
@property (nonatomic, strong) UIView *navigationView;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UIButton *shareButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *lineLabel;
@end

@implementation FLLSceneInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _pageCount = 2;
    [self createinfoTableView];
    [self createPOSTRequest];
    // 自定义导航栏
    [self createNavigationView];
    [self createHeaderView];
    
    
    
   }

- (void)createNavigationView
{

    self.navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    self.navigationView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_navigationView];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setImage:[UIImage imageNamed:@"nav_back_white"] forState:UIControlStateNormal];
    [self.navigationView addSubview:_backButton];
    [self.backButton addTarget:self action:@selector(didClickedBackButtton:) forControlEvents:UIControlEventTouchUpInside];
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.height.width.mas_equalTo(40);
        make.top.mas_equalTo(22);
    }];
    
    self.titleLabel = [[UILabel alloc] init];
    [self.titleLabel setPropetyWithTextColor:[UIColor whiteColor] Alignment:1 textFont:[UIFont systemFontOfSize:17 * kWidthScale]];
    [self.navigationView addSubview:_titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_backButton.mas_centerY);
        make.height.mas_equalTo(_backButton.mas_height);
        make.centerX.mas_equalTo(0);
    }];
    
    
    self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shareButton setImage:[UIImage imageNamed:@"ico_share_white"] forState:UIControlStateNormal];
    [self.shareButton addTarget:self action:@selector(didClickedShareButtton:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationView addSubview:_shareButton];
    [self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.height.width.mas_equalTo(_backButton.mas_width).multipliedBy(0.5);
        make.centerY.mas_equalTo(_backButton.mas_centerY);
    }];

    self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.searchButton setImage:[UIImage imageNamed:@"searchWhite"] forState:UIControlStateNormal];
    [self.searchButton addTarget:self action:@selector(didClickedSearchButtton:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationView addSubview:_searchButton];
    [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_shareButton.mas_left).mas_offset(-10);
        make.height.width.mas_equalTo(_backButton.mas_width);
        make.centerY.mas_equalTo(_backButton.mas_centerY).mas_offset(5);
    }];
    
    self.lineLabel = [[UILabel alloc] init];
    [self.navigationView addSubview:_lineLabel];
    self.lineLabel.backgroundColor = [UIColor clearColor];
    [self.lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    


    
    
}
- (void)createPOSTRequest
{
    if ([self.idString hasSuffix:@"#"]) {
         self.idString = (NSMutableString *)[self.idString componentsSeparatedByString:@"#"][1];
    }
    self.bodyString = [NSString stringWithFormat:@"scene_id=%@&page=1&methodName=SceneInfo&size=20&version=4.40&", _idString];
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1"
    andBody:_bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        [self.tableArray removeAllObjects];
    self.fllInfoModel = [FLLInfoModel modelWithDic:result];
    self.tableArray = (NSMutableArray *)self.fllInfoModel.data.dishes_list;
    self.headerBlock();
    [self.infoTableView reloadData];
        } andFailureBlock:^(NSError *error) {
                 
             }];
}

// 加载
- (void)createLoadingRequest
{

    if ([self.idString hasSuffix:@"#"]) {
        self.idString = (NSMutableString *)[self.idString componentsSeparatedByString:@"#"][1];
    }
    NSString *bodyString = [NSString stringWithFormat:@"scene_id=%@&page=%zd&methodName=SceneDishes&size=20&version=4.40&", _idString, _pageCount];
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1"
        andBody:bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
            
        _pageCount++;
        self.fllAllDishModel = [FLLAllDishModel modelWithDic:result];
        [self.tableArray addObjectsFromArray:(NSMutableArray *)self.fllAllDishModel.data.data];
            self.headerBlock();
        [self.infoTableView reloadData];
             } andFailureBlock:^(NSError *error) {
                 
        }];

}
- (void)createinfoTableView
{

    self.infoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.infoTableView.delegate = self;
    self.infoTableView.dataSource = self;
    self.infoTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.infoTableView registerNib:[UINib nibWithNibName:@"FLLInfoTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"infoTableCell"];
    [self.view addSubview:_infoTableView];
    
    // 下拉刷新
    self.infoTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _isLoading = NO;
        [self createPOSTRequest];
        [self.infoTableView.mj_header endRefreshing];
    }];
    // 上拉加载
    self.infoTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _isLoading = YES;
        [self createLoadingRequest];
        [self.infoTableView.mj_footer endRefreshing];
    }];
    
    
    
}
- (void)createHeaderView
{

    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.68 * SCREEN_WIDTH)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:_headerView.bounds];
    [self.headerView addSubview:imageView];
    UIImageView *blackView = [[UIImageView alloc] initWithFrame:_headerView.bounds];
    blackView.alpha = 0.5;
    [self.headerView addSubview:blackView];
    blackView.image = [UIImage imageNamed:@"detailbg"];
                              
    
#warning mark -- 为什么用masonary约束就添加不上?????????
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 100 ) / 2.0, CGRectGetHeight(_headerView.bounds) * 3 / 4.0, 100, 25)];
    
    
    [titleLabel setPropetyWithTextColor:[UIColor whiteColor] Alignment:1 textFont:[UIFont systemFontOfSize:13 * kWidthScale]];
    [self.headerView addSubview:titleLabel];

    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - SCREEN_WIDTH * 5 / 7.0 ) / 2.0, CGRectGetHeight(_headerView.bounds) * 3 / 4.0 + 30, SCREEN_WIDTH * 5 / 7.0, 25)];
    [contentLabel setPropetyWithTextColor:[UIColor whiteColor] Alignment:1 textFont:[UIFont systemFontOfSize:11 * kWidthScale]];
    [self.headerView addSubview:contentLabel];
    
    __weak FLLSceneInfoViewController *weakSelf = self;
    // 用block赋值
    self.headerBlock = ^(){
        titleLabel.text = [NSString stringWithFormat:@"%zd道菜", weakSelf.fllInfoModel.data.dish_count];
        contentLabel.text = weakSelf.fllInfoModel.data.scene_desc;
        [imageView sd_setImageWithURL:[NSURL URLWithString:weakSelf.fllInfoModel.data.scene_background] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        weakSelf.titleLabel.text = weakSelf.fllInfoModel.data.scene_title;
        
    
    };
    
  
    self.infoTableView.tableHeaderView = _headerView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _tableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    FLLInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"infoTableCell"];
    self.collectionArray = (NSMutableArray *)self.fllInfoModel.data.dishes_list[indexPath.row].tags_info;
    [cell.dishImageView sd_setImageWithURL:[NSURL URLWithString:_tableArray[indexPath.row].image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    cell.titleLabel.text = _tableArray[indexPath.row].dishes_name;
    cell.contentLabel.text = _tableArray[indexPath.row].dishes_desc;
    
    cell.collectionArray = _collectionArray;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return SCREEN_HEIGHT * 0.2;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    if (scrollView.contentOffset.y >= 90 * kHeightScale) {

        self.navigationView.backgroundColor = [UIColor whiteColor];
        [self.backButton setImage:[UIImage imageNamed:@"nav_back_black"] forState:UIControlStateNormal];
        self.titleLabel.textColor = [UIColor blackColor];
        [self.searchButton setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
        [self.shareButton setImage:[UIImage imageNamed:@"分享-1"] forState:UIControlStateNormal];
        self.lineLabel.backgroundColor = [UIColor lightGrayColor];
       
    } if (scrollView.contentOffset.y < 90 * kHeightScale) {
    
        self.navigationView.backgroundColor = [UIColor clearColor];
        [self.backButton setImage:[UIImage imageNamed:@"nav_back_white"] forState:UIControlStateNormal];
        self.titleLabel.textColor = [UIColor whiteColor];
        [self.searchButton setImage:[UIImage imageNamed:@"searchWhite"] forState:UIControlStateNormal];
        [self.shareButton setImage:[UIImage imageNamed:@"ico_share_white"] forState:UIControlStateNormal];
        self.lineLabel.backgroundColor = [UIColor clearColor];
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    // 传id
    FLLMakeFoodViewController *makeFoodVC = [[FLLMakeFoodViewController alloc] init];
    makeFoodVC.idString = [NSString stringWithFormat:@"%zd", _tableArray[indexPath.row].dishes_id];
    [self.navigationController pushViewController:makeFoodVC animated:YES];
    
}
// 导航栏隐藏及出现
- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    self.navigationView.backgroundColor = [UIColor clearColor];
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{

    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationView.backgroundColor = [UIColor whiteColor];
}


// 返回按钮
- (void)didClickedBackButtton:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
// 搜索按钮
- (void)didClickedSearchButtton:(UIBarButtonItem *)button
{
    FLLSearchViewController *search = [[FLLSearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
    
}
// 分享按钮
- (void)didClickedShareButtton:(UIBarButtonItem *)button
{
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
